/**
 * Created by 张其川CheungSSH on 2016/11/15.
 */


var menu = [
    {
        "创建用户": {
            "class": "glyphicon glyphicon-globe",
            "id": "createUser"
        }
    },
    {
        "用户管理": {
            "class": "glyphicon glyphicon-move",
            "id": "userAdmin"
        }
    },

    {
        "系统激活": {
            "class": "glyphicon glyphicon-bookmark",
            "id": "activate"
        }
    },
    {
        "页面管理": {
            "class": "glyphicon glyphicon-signal",
            "id": "webAdmin"
        }
    },
    {
        "习题管理": {
            "class": "glyphicon glyphicon-eur",
            "id": "xiti"
        }
    },
	/*
    {
        "业务操作": {
            "class": "glyphicon glyphicon-briefcase",
            "id": "serviceOperation"

        }
    }, */
	
    {
        "序列号管理": {
            "class": "glyphicon glyphicon-facetime-video",
            "id": "audit",
            "subMenu": {
                "序列号类型": {
                    "class": "glyphicon glyphicon-console",
                    "id": "s-type"
                },
                "序列号添加": {
                    "class": "glyphicon glyphicon-book",
                    "id": "serialAdd"
                },
                "批量添加记录表": {
                    "class": "glyphicon glyphicon-leaf",
                    "id": "batchSerialList"
                },
                "单个添加记录表": {
                    "class": "glyphicon glyphicon-random",
                    "id": "singleSerialList"
                },
                "序列号查询": {
                    "class": "glyphicon glyphicon-globe",
                    "id": "querySerial"
                },
            }
        }

    },

	/*
    {
        // "设置": ["命令黑名单", "登录阈值"]
        "命令拦截": {
            "class": "glyphicon glyphicon-cog",
            "id": "settingsMenu",
            "subMenu": {
                "命令黑名单": {
                    "class": "glyphicon glyphicon-eye-open",
                    "id": "commandBlack"
                }
                ,
                "登录阈值": {
                    "class": "glyphicon glyphicon-eye-close",
                    "id": "loginLimit"
                }
            }
        }

    },
    {
        "远程文件": {
            "class": "glyphicon glyphicon-folder-open",
            "id": "remoteFile",
            "subMenu": {
                "文件": {
                    "class": "glyphicon glyphicon-list-alt",
                    "id": "catRemoteFile",
                },
            },
        },
    },
	*/
	/*
	
    {
        "代码发布": {
            "class": "glyphicon glyphicon-briefcase",
            "id": "appDeploy",
            "subMenu":{
                  "灰度发布":{
 			"class":"glyphicon glyphicon-credit-card",
			"id":"singleMode",
		   },
		   "批量发布":{
			"class":"glyphicon glyphicon-random",
			"id":"batchMode",
			}
            }
        }
    }, */
	/*
    {
        "发布计划": {
            "class": "glyphicon glyphicon-blackboard",
            "id": "deploymentCrontab",
        }
    }, */


];

$(document).on("click","#xiti",function(){
    window.location.href="../html/admin.html?page=inventory.html";
})
$(document).on("click","#querySerial",function(){
    window.location.href="../html/admin.html?page=querySerial.html";
})
$(document).on("click","#webAdmin",function(){
    window.location.href="../html/admin.html?page=webAdmin.html";
})
$(document).on("click","#singleSerialList",function(){
    window.location.href="../html/admin.html?page=singleSerialList.html";
})
$(document).on("click","#batchSerialList",function(){
    window.location.href="../html/admin.html?page=batchSerialList.html";
})

$(document).on("click","#serialAdd",function(){
    window.location.href="../html/admin.html?page=serialAdd.html";
})

$(document).on("click","#s-type",function(){
    window.location.href="../html/admin.html?page=s-type.html";
})
$(document).on("click","#activate",function(){
    window.location.href="../html/admin.html?page=activate.html";
})
$(document).on("click","#createUser",function(){
    window.location.href="../html/admin.html?page=createUser.html";
    sectionColor(this);
})
$(document).on("click","#userAdmin",function(){
    window.location.href="../html/admin.html?page=userAdmin.html";
    sectionColor(this);
})
function createMenu() {
    var menuDiv = document.getElementById("menu");
    for (var i = 0; i < menu.length; i++) {
        var section = menu[i];
        for (funcName in section) {
            var icon = section[funcName].class;
            var id = section[funcName].id
            var isSub = section[funcName].subMenu;
            var divFunc = document.createElement("div");
            var divLine = document.createElement("div");


            divLine.style.cssText = 'padding:10px;font-size:12px;cursor:pointer;position:relative'
            divLine.setAttribute("id", id);
            divLine.className = "sectionLine"
            var divPic = document.createElement("span");
            divPic.style.fontSize = "120%";
            divPic.className = icon;

            var divText = document.createElement("span");
            divText.textContent = funcName;
            divText.style.cssText = "font-size: 120%;margin-left: 15px;";


            divLine.appendChild(divPic);
            divLine.appendChild(divText);

            if (isSub) {
                divLine.onclick = function () {
                    showAndCloseSection(this);

                }
                var spanRight = document.createElement("span");
                spanRight.className = "fa fa-angle-right fa-2x ";
                spanRight.style.cssText = "right:10px;position:absolute;"
                divLine.appendChild(spanRight);
            }


            divFunc.appendChild(divLine);


            //子功能
            if (isSub) {
                var divSub = document.createElement("div");
                divSub.style.display = "none";
                for (subSectionName in  isSub) {
                    var id = isSub[subSectionName].id;
                    var icon = isSub[subSectionName].class;

                    var subDivLine = document.createElement("div");
                    subDivLine.style.cssText = 'padding:10px;font-size:12px;cursor:pointer;'
                    subDivLine.className = "sectionLine"
                    subDivLine.setAttribute("id", id);


                    var pic = document.createElement("span");
                    pic.className = icon;
                    pic.style.fontSize = "120%";

                    //字体部分
                    var text = document.createElement("span");
                    text.style.cssText = "font-size: 120%;margin-left: 15px;";
                    text.textContent = subSectionName;
                    text.style.cssText = "font-size: 120%;margin-left: 15px;";
                    subDivLine.appendChild(pic);
                    subDivLine.appendChild(text);
                    divSub.appendChild(subDivLine);


                }

                divFunc.appendChild(divSub);

            }


            menuDiv.appendChild(divFunc);


        }

    }
}


function screenFull() {
    //设置全屏高度
    document.getElementById("allMain").style.height = window.innerHeight + "px";
    //设置子页面和菜单的
    document.getElementById("menuAndSonePage").style.height = window.innerHeight - 50 + "px";
    //设置子页面的宽度
    document.getElementById("showMainContent").style.width = window.innerWidth - 190 + "px";
}

window.onresize = function () {
    screenFull();
}

$(function () {
    screenFull();
    createMenu();
    //绑定菜单关闭
    jQuery1_8("#showMenu").toggle(
        function () {
            $("#menu").animate({
                "left": "-180px",
            }, 300, function () {
                $("#showMainContent").css({
                    "position": "absolute",
                    "width": window.innerWidth + "px",
                })
            });
        }, function () {
            $("#showMainContent").css({
                "position": "relative",
                "width": window.innerWidth - 180 + "px",
            })
            $("#menu").animate({
                "left": "0px",
            }, 300);
        }
    )


})


//老版本js


$(function () {
    $("#powerOff").click(function () {
        CheungSSHLogout();
    });
})


function CheungSSHLogout() {
    jQuery.ajax({
        "url": headURL + logoutURL,
        "type": "get",
        "error": errorAjax,
        "beforeSend": start_load_pic,
        "complete": stop_load_pic,
        "success": function (data) {
            window.location.href = "/cheungssh/admin";
        }
    });
}

function CheungSSHLogin(username, password) {
	console.log(666)
    jQuery.ajax({
        "url": "/super_login/",
        "type": "POST",
        "error": errorAjax,
        "success": responseCheck,
        "beforeSend": start_load_pic,
        "complete": stop_load_pic,
        "data": {"username": username, "password": password}
    });
}


function searchValue(input) {

    var searchValue = input.value.toLowerCase();
    var table = $("table").find("tbody tr");
    table.each(
        function () {
            // if(!searchValue)return false;
            var e = jQuery(this);
            var eValue = e.text().toLowerCase();
            if (!eValue.match(searchValue)) {
                e.hide();
            }
            else {
                e.show()
            }
        }
    );

};


function sectionColor(div) {
    //start_load_pic();
    $("#menu").find(".sectionLine").css({"background": ""});
	console.log(div)
    div.style.background = "#09c"
}

$(document).on('keyup', '.searchValue', function () {
    searchValue(this);
});


$(function () {

    var browserInfo = navigator.userAgent.toLowerCase();

    if (!browserInfo.match(/webkit/)) {
        document.getElementById("showWarnContent").innerHTML = "很抱歉,您当前必须使用谷歌内核的浏览器操作CheugnSSH系统！" +
            "<br/> <a   id='showDetail' href='#' style='display:block;color:blue;font-weight: normal;'>详细信息 </a>  <span id='showDetailContent' style='display:none;color:black;font-weight: normal;'>1.支持带有谷歌内核的浏览器<br/>2.360浏览器的极速模式</span>";
        $(".closeDiv").show();

    }


});





function showSuccessNotic(info) {
    var element = "";
    if (window.innerWidth > 737) {
        var t = document.getElementById("showSuccessNotic");
        if (info !== undefined) {
            //后端额外附加信息
            t.textContent = "操作成功," + info + "!";
        }
        else {
            t.textContent = "操作成功!";
        }
        $("#showSuccessNotic").slideDown("fast");
    }
    else {
        var t = document.getElementById("showSuccessNoticeIphone");
        t.style.display = "block";
    }
    element = t;
    setTimeout(function () {
        //element.style.display = "none"
        $("#showSuccessNotic").slideUp("slow");
    }, 2000)//三秒钟过后，自动消失

}

function showAndCloseSection(div) {
    var nextDiv = $(div).next();
    if (nextDiv[0].style.display === "none") {
        $(div).parent().find(".fa").removeClass("fa-angle-right").addClass("fa-angle-down")
        $(nextDiv).slideDown("fast");
    }
    else {
        $(nextDiv).slideUp("fast");
        $(this).removeClass("fa-angle-right")
        $(div).parent().find(".fa").removeClass("fa-angle-down").addClass("fa-angle-right")
    }
}


function goPage(){
	var data=getQ()
	if (data.page!==undefined){
		$("#showMainContent").load("../html/"+data.page)
	}
	var div=data.page.split(".")[0]
	div=document.getElementById(div)
    	sectionColor(div);
	
}
$.ajax({
	"url":"/web_name/",
	"type":"GET",
	"dataType":"jsonp",
	"async":false,
	"error":errorAjax,
	"success":function(data){
		$("#webName").text(data.content)
	}
})

$(function () {
    //全局初始化 ,加载需要提前加载的数据
    //绑定后台登录页面
    //绑定点击通知面板影藏
    document.getElementById("loginNotifyPannel").onclick=function(){
        $(this.parentNode).animate({
		"bottom":"-200px"
        },function(){
		this.style.display="none";
        })
    }

	//转到页面
	goPage()





})
